﻿#region Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using eSilky.VO;
#endregion

namespace eSilky.DAL
{
    public class Product
    {
        #region Get A Product
        /// <summary>
        ///  Get details of a product
        /// </summary>
        /// <param name="pdt">Product code</param>
        /// <returns>Dataset</returns>
        public DataSet GetAProduct(int productid)
        {
            OleDbConnection con;
            string qryString;
            OleDbDataAdapter da;
            DataSet ds;

            try
            {
                con = dBConnection.GetConnection();
                qryString = "select pdt_id,pdt_code,pdt_name,pdt_description,cat_id,pdt_weight,pdt_rate,pdt_discount,pdt_status,pdt_image from esilky_product where pdt_id=@productId";
                da = new OleDbDataAdapter(qryString, con);
                da.SelectCommand.Parameters.Add(new OleDbParameter("@productId", productid));
                ds = new DataSet();
                da.Fill(ds);
                return (ds);
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region Get All Product In A Category
        /// <summary>
        ///  Get detais of all the products of a category 
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="categoryid"></param>
        /// <returns></returns>
        public DataSet GetAllProduct(int categoryid)
        {
            OleDbConnection con;
            string qryString;
            OleDbCommand cmd;
            OleDbDataAdapter da;
            DataSet ds;

            try
            {
                con = dBConnection.GetConnection();
                qryString = "select pdt_id,pdt_code,pdt_name,pdt_description,cat_id,pdt_weight,pdt_rate,pdt_discount,pdt_status,pdt_image from esilky_product where cat_id=@CategoryId";
                da = new OleDbDataAdapter(qryString, con);
                da.SelectCommand.Parameters.Add(new OleDbParameter("@CategoryId", categoryid));
                ds = new DataSet();
                da.Fill(ds);
                return (ds);
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region Add a new product
        public int GetAllProduct(VO.Product pdt)
        {
            OleDbConnection con;
            string qryString;
            OleDbCommand cmd;
            DataSet ds;

            try
            {
                con = dBConnection.GetConnection();

                qryString = "INSERT INTO esilky_product "
                    + "(pdt_code, pdt_name, pdt_description, cat_id, pdt_weight, pdt_rate, pdt_discount, pdt_status, pdt_image, created_on) "
                    + "VALUES(@code,@name,@description,@categoryid,@weight,@rate,@discount,@status,@image,getdate())";
                cmd = new OleDbCommand(qryString, con);
                cmd.Parameters.Add(new OleDbParameter("@code", pdt.code));
                cmd.Parameters.Add(new OleDbParameter("@name", pdt.name));
                cmd.Parameters.Add(new OleDbParameter("@description", pdt.description));
                cmd.Parameters.Add(new OleDbParameter("@categoryid", pdt.categoryid));
                cmd.Parameters.Add(new OleDbParameter("@weight", pdt.weight));
                cmd.Parameters.Add(new OleDbParameter("@rate", pdt.rate));
                cmd.Parameters.Add(new OleDbParameter("@discount", pdt.discount));
                cmd.Parameters.Add(new OleDbParameter("@status", pdt.status));
                cmd.Parameters.Add(new OleDbParameter("@image", pdt.image));
                ds = new DataSet();

                return (cmd.ExecuteNonQuery());
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region Update a product

        #endregion

        #region Remove a product

        #endregion
    }
}